package Chen.rbac1.view ;

import Chen.rbac1.domain.vo.DeptVo;
import Chen.rbac1.domain.dto.DeptDto;
import Chen.rbac1.domain.vo.Result;
import Chen.rbac1.domain.service.DeptService;

import java.util.Scanner;

import static Chen.rbac1.view.MainView.input;

public class DeptView {

    public static void show() {
    }

    public class Deptview {
        /**
         * 视图对象缓存数组
         */
        public static DeptVo[] cache;

        public static void show() {
            String menuChoice;
            do {
                printMenu();
                menuChoice = MainView.input.nextLine();
                switch (menuChoice) {
                    case "0":
                        System.out.println("已返回上级菜单！");
                    case "1":
                        list();
                        break;
                    case "2":
                        create();
                        break;
                    case "3":

                        break;
                    case "4":

                        break;
                    default:
                        System.out.println("输入非法，请输入正确的菜单编号！");
                }
            } while (!"0".equals(menuChoice));
            MainView.input.close();
        }

        public static void printMenu() {
            String menu = """
                    --------------
                        RBAC系统
                    --------------
                     | 0 返回上级 |
                     | 1 部门列表 |
                     | 2 创建部门 |
                     | 3 更新部门 |
                     | 4 删除部门 |
                    --------------
                    请输入选项：""";
            System.out.print(menu);
        }

        public static void list() {
            Result result = DeptService.list();
            cache = (DeptVo []) result.data;
            System.out.println("ID\t部门名称\t部门电话\t");
            for (DeptVo deptvo : cache) {
                System.out.printf("%d\t%s\t%s\t\n", deptvo.id, deptvo.name, deptvo.phone);
            }
        }

        public static void create() {
            System.out.println("请输入部门名称:");
            String name = MainView.input.nextLine();
            System.out.println("请输入部门电话:");
            String phone = MainView.input.nextLine();
            DeptDto dto = new DeptDto(name, phone);
            Result result = DeptService.create(dto);
            if (!result.success) {
                System.err.println(result.message);
                return;
            }
            System.out.println(result.message);
            list();
        }

        public static void update() {
            System.out.println("请输入ID,ID必须是大于0的整数:");
        }
    }
}